package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {

        PageHelper.startPage(page,rows);
        Page<Supplier> pageSupplier = supplierDao.selectPage(supplierName);
        Map<String, Object> map = new HashMap<>();
        map.put("total",pageSupplier.getTotal());
        map.put("rows",pageSupplier);
        return map;
    }

    /**
     * 供应商添加或修改
     * @param supplier
     * @return
     */
    @Override
    public ServiceVO saveOrUpdate(Supplier supplier) {
        Integer supplierId = supplier.getSupplierId();
        Supplier supplier1 =supplierDao.selectSupplier(supplierId);
        if (null==supplier1){
            //添加
            supplierDao.saveSupplier(supplier);
        }else {

            //修改
            supplierDao.update(supplier);
        }
        return new ServiceVO(100,"请求成功");

    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public ServiceVO delete(String[] ids) {
        for (String id : ids) {
            supplierDao.delete(id);
        }
        return new ServiceVO(100,"请求成功");
    }
}
